package noticeController

import (
	"CMETO/models"
	"CMETO/utils"
	"net/http"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

type NoticeController struct{}

// @Summary 通知查询
// @Description 查询通知
// @Tags 通知管理
// @Accept json
// @Produce json
// @Param page query string false "页码"
// @Param pageSize query string false "尺寸"
// @Param name query string false "通知名"
// @Success 200 {object} models.ResultCommon[[]models.Notice] "查询成功"
// @Success 400 {object} models.ErrorResult "查询失败"
// @Router /notice [get]
// @Security JWT
func (notice NoticeController) QueryNotices(ctx *gin.Context) {
	page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(ctx.DefaultQuery("pageSize", "10"))
	nameStr := ctx.DefaultQuery("name", "")
	name := "%" + nameStr + "%"
	noticeList := []models.NoticeFormat{}
	models.DB.Model(&models.Notice{}).
		Select("notices.*,users.username as create_user").
		Joins("LEFT JOIN users ON notices.create_user_id = users.id").
		Where("notices.is_delete!=1 AND notices.title like ?", name).Limit(pageSize).Offset(pageSize * (page - 1)).
		Scan(&noticeList)
	var total int64
	models.DB.Table("notices").Where("is_delete!=1 AND title like ?", name).Count(&total)
	result := &models.ResultByPage[models.NoticeFormat]{
		Msg:      "ok",
		Code:     "200",
		Total:    total,
		Page:     page,
		PageSize: pageSize,
		Result:   noticeList,
	}
	ctx.JSON(http.StatusOK, result)
}

// @Summary 通知新增
// @Description 新增通知
// @Tags 通知管理
// @Accept json
// @Produce json
// @Param deptInfo body models.Notice true "通知信息"
// @Success 200 {object} models.AddResult "新增成功"
// @fail 400 {object} models.ErrorResult "数据错误"
// @fail 401 {object} models.AddResult "新增失败"
// @Router /notice [post]
// @Security JWT
func (d NoticeController) AddNotice(ctx *gin.Context) {
	notice := &models.Notice{}
	// 绑定json数据
	if err := ctx.ShouldBindJSON(&notice); err != nil {
		ctx.JSON(http.StatusOK, &models.ErrorResult{
			Msg:    "数据错误",
			Code:   "400",
			Result: nil,
		})
	} else {
		notice.CreateTime = utils.TimeFormatter(time.Now())
		notice.UpdateTime = utils.TimeFormatter(time.Now())
		var result = models.DB.Model(&models.Notice{}).Create(&notice).RowsAffected
		// 返回
		if result != 1 {
			ctx.JSON(http.StatusOK, &models.AddResult{
				Msg:    "新增失败",
				Code:   "400",
				Result: result,
			})
		} else {
			ctx.JSON(http.StatusOK, &models.AddResult{
				Msg:    "新增成功",
				Code:   "200",
				Result: result,
			})
		}
	}
}

// @Summary 通知修改
// @Description 修改通知
// @Tags 通知管理
// @Accept json
// @Produce json
// @Param deptInfo body models.Notice true "通知信息"
// @Success 200 {object} models.UpdateResult[models.Notice] "更新成功"
// @fail 400 {object} models.ErrorResult "数据错误"
// @fail 401 {object} models.AddResult "修改失败"
// @Router /notice [put]
// @Security JWT
func (d NoticeController) UpdateNotice(ctx *gin.Context) {
	var notice models.Notice
	// 绑定json数据
	if err := ctx.ShouldBindJSON(&notice); err != nil {
		ctx.JSON(http.StatusOK, &models.ErrorResult{
			Msg:    "数据绑定失败",
			Code:   "400",
			Result: err,
		})
		return
	}
	notice_old := &models.Notice{}

	models.DB.First(&notice_old, notice.Id)
	notice.UpdateTime = utils.TimeFormatter(time.Now())
	if notice_old.Id == 0 {
		ctx.JSON(http.StatusOK, &models.ErrorResult{
			Msg:    "更新失败",
			Code:   "400",
			Result: nil,
		})
		return
	}
	result := models.DB.Model(&notice_old).Save(&notice)
	if result.RowsAffected == 0 {
		ctx.JSON(http.StatusOK, &models.ErrorResult{
			Msg:    "更新失败",
			Code:   "400",
			Result: nil,
		})
	} else {
		ctx.JSON(http.StatusOK, &models.UpdateResult[models.Notice]{
			Msg:    "更新成功",
			Code:   "200",
			Result: notice,
		})
	}
}

// @Summary 删除通知
// @Description 传入json，批量删除通知
// @Tags 通知管理
// @Accept json
// @Produce json
// @Param deptsIds body models.DeleteParams true "通知id合集"
// @Success 200 {object} models.DeleteResult "更新成功"
// @fail 400 {object} models.ErrorResult "数据错误"
// @fail 401 {object} models.DeleteResult "删除失败"
// @Router /notice [delete]
// @Security JWT
func (d NoticeController) DeleteNotice(ctx *gin.Context) {
	// 绑定参数实例
	ids := &models.DeleteParams{}
	// 定义模型实例
	var notice models.Notice
	if err := ctx.ShouldBindJSON(&ids); err != nil {
		ctx.JSON(http.StatusOK, &models.ErrorResult{
			Msg:    "删除失败",
			Code:   "400",
			Result: err,
		})
	} else {
		// 假的删
		result := models.DB.Model(&notice).Where("id IN (?)", ids.Ids).Update("is_delete", 1)
		if result.RowsAffected != 0 {
			ctx.JSON(http.StatusOK, &models.DeleteResult{
				Msg:    "删除成功",
				Code:   "200",
				Result: ids.Ids,
			})
		} else {
			ctx.JSON(http.StatusOK, &models.DeleteResult{
				Msg:    "删除失败",
				Code:   "400",
				Result: []int{0},
			})
		}
	}
}
